<?php

/**
 * Member Controller
 * ระบบ member
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Member system
 * ระบบต่างๆของสมาชิก
 * @package Member
 * @subpackage Controller
 * @category member
 * @author BeYourCyber
 * @version 1.0
 * @todo ให้สมาชิกแก้ไขข้อมูลตัวเองได้
 * 
 */
class Mem extends MY_Controller {

    function __construct() {
        parent::MY_Controller();
        $this->load->library('tank_auth');
        $this->load->module('theme');
        
//        $this->theme->library('theme_lib');
        
    }

    function index(){
        
    }
    /**
     * แก้ไขข้อมูลส่วนตัว
     * @param int userid
     * @uses Member_m::getUserId()
     * @uses Group_m::getAllGroup()
     */
    function manage(){
        $cl = $this->__modulereference;
        $this->$cl->lang('mem');
        if ($this->tank_auth->is_logged_in()) {
            $id = intval($this->uri->segment(5));
            if(is_int($id) && ($id>0) && ($id == $this->tank_auth->get_user_id())){
                //load all profile
                $data->profile = $this->$cl->model->member_m->getUserId($id);
                ////load all group
                $data->group = $this->$cl->model->group_m->getAllGroup();
                
                $this->$cl->view('front/manage_v',$data);
            }
            
        } else {
            redirect('auth/login');
        }
    }

    /**
     * submit to edit profile
     * @param int userid
     * @param array user data
     * @uses Member_m::updateMemberProfile()
     */
    function edit_profile(){
        $cl = $this->__modulereference;
        $this->$cl->lang('mem');
        if ($this->tank_auth->is_logged_in()) {
            $id = intval($this->uri->segment(5));
            if(is_int($id) && ($id>0) && ($id == $this->tank_auth->get_user_id())){
                foreach (explode(' ', 'country website') as $v) {
                    $data[$v] = $this->input->post($v);
                }
                $this->$cl->model->member_m->updateMemberProfile($data,$id);
                $this->bee->redirectBack();
            }

        } else {
            redirect('auth/login');
        }
    }

    function change_password(){
        $cl = $this->__modulereference;
        $this->$cl->lang('mem');
        if ($this->tank_auth->is_logged_in()) {
            $id = intval($this->uri->segment(5));
            if(is_int($id) && ($id>0) && ($id == $this->tank_auth->get_user_id())){
                foreach (explode(' ', 'country website') as $v) {
                    $data[$v] = $this->input->post($v);
                }
                $this->$cl->model->member_m->updateMemberProfile($data,$id);
                $this->bee->redirectBack();
            }

        } else {
            redirect('auth/login');
        }
    }


}
